<?php

class Exam_Widget_ListingsExamController extends Engine_Content_Widget_Abstract {

  public function indexAction() {

    $viewer = Engine_Api::_()->user()->getViewer();
    $this->view->viewer_id = $viewer->getIdentity();
//    $this->view->list_browse = $list_browse = Zend_Registry::get('list_browse');

    $ShowViewArray = Engine_Api::_()->getApi('settings', 'core')->getSetting('exam.ajax.widgets.layout', array("0" => "1", "1" => "2", "2" => "3"));
    $defaultOrder = Engine_Api::_()->getApi('settings', 'core')->getSetting('exam.ajax.layouts.oder', 1);
    $this->view->list_view = 0;
    $this->view->grid_view = 0;
    $this->view->map_view = 0;
    $this->view->defaultView = -1;
//    $ShowViewArray = array('0'=>'1');
    if (in_array("1", $ShowViewArray)) {
      $this->view->list_view = 1;
      if ($this->view->defaultView == -1 || $defaultOrder == 1)
        $this->view->defaultView = 0;
    }
    if (in_array("2", $ShowViewArray)) {
      $this->view->grid_view = 1;
      if ($this->view->defaultView == -1 || $defaultOrder == 2)
        $this->view->defaultView = 1;
    }
    if (in_array("3", $ShowViewArray)) {
      $this->view->map_view = 1;
      if ($this->view->defaultView == -1 || $defaultOrder == 3)
        $this->view->defaultView = 2;
    }

//    if ($this->view->defaultView == -1) {
//      return $this->setNoRender();
//    }
    $customFieldValues = array();
    $values = array();

    $category = Zend_Controller_Front::getInstance()->getRequest()->getParam('category_id', null);
    $subcategory = Zend_Controller_Front::getInstance()->getRequest()->getParam('subcategory_id', null);

    //GET VALUE BY POST TO GET DESIRED EXAMS  

    if (!empty($_GET)) {
      $values = $_GET;
    }

    $form = new Exam_Form_Search();
    if (!empty($_GET))
      $form->populate($_GET);
    $values = $form->getValues();

    if (($category) != null) {
      $values['category'] = $category;
      $values['subcategory'] = $subcategory;
    }


    if (!empty($_GET['page'])) {
      $values['page'] = $_GET['page'];
    } else {
      $values['page'] = 1;
    }

    // GET LISITNG FPR PUBLIC PAGE SET VALUE
//    $values['type'] = 'browse';
//    if (isset($_GET['city']) && !empty($_GET['city'])) {
//      $city = $_GET['city'];
//      unset($_GET);
//      $_GET['city'] = $city;
//    }
//    if (isset($_GET['tag']) && !empty($_GET['tag'])) {
//      $tag = $_GET['tag'];
//      unset($_GET);
//      $_GET['tag'] = $tag;
//    }
    // Do the show thingy
//    if (@$values['show'] == 2) {

      // Get an array of friend ids to pass to getListsPaginator
//      $table = Engine_Api::_()->getItemTable('user');
//      $select = $viewer->membership()->getMembersSelect('user_id');
//      $friends = $table->fetchAll($select);

      // Get stuff
//      $ids = array();
//      foreach ($friends as $friend) {
//        $ids[] = $friend->user_id;
//      }

      //unset($values['show']);
//      $values['users'] = $ids;
//    }
//    $this->view->assign($values);

    // Get the result for enable rating
    $this->view->ratngShow = $ratingShow = (int) Engine_Api::_()->getApi('settings', 'core')->getSetting('exam.rating', 1);
    // Get the custome Fields
//    $customFieldValues = array_intersect_key($values, $form->getFieldElements());
//    if ($form->show->getValue() == 3 && !isset($_GET['show'])) {
//      @$values['show'] = 3;
//    }
    
//    $coremodule = Engine_Api::_()->getDbtable('modules', 'core')->getModule('core');
//    $coreversion = $coremodule->version;
//    if ($coreversion > '4.1.7') {
//       // Process options
//      $tmp = array();
//      foreach ($customFieldValues as $k => $v) {
//        if (null == $v || '' == $v || (is_array($v) && count(array_filter($v)) == 0)) {
//          continue;
//        } else if (false !== strpos($k, '_field_')) {
//          list($null, $field) = explode('_field_', $k);
//          $tmp['field_' . $field] = $v;
//        } else if (false !== strpos($k, '_alias_')) {
//          list($null, $alias) = explode('_alias_', $k);
//          $tmp[$alias] = $v;
//        } else {
//          $tmp[$k] = $v;
//        }
//      }
//      $customFieldValues = $tmp;
//    }
    
    // GET EXAMS
    $table = Engine_Api::_()->getDbTable('exams','exam');
    $select = $table->select();
    $this->view->paginator = $paginator = Zend_Paginator::factory($select);
//    $this->view->paginator = $paginator = Engine_Api::_()->exam()->getListsPaginator($values, $customFieldValues);

    $items_count = (int) Engine_Api::_()->getApi('settings', 'core')->getSetting('exam.page', 10);
    $paginator->setItemCountPerPage($items_count);
    $this->view->paginator = $paginator->setCurrentPageNumber($values['page']);
//    $this->view->list_generic = Zend_Registry::get('list_generic');

//    $this->view->enableLocation = $checkLocation = Engine_Api::_()->list()->enableLocation();
//    $this->view->flageSponsored = 0;

//    if (!empty($checkLocation) && $paginator->getTotalItemCount() > 0) {
//      $ids = array();
//      $sponsored = array();
//      foreach ($paginator as $list) {
//        $id = $list->getIdentity();
//        $ids[] = $id;
//        $list_temp[$id] = $list;
//      }
//      $values['listing_ids'] = $ids;
//      $this->view->locations = $locations = Engine_Api::_()->list()->getLocation($values);
//
//      foreach ($locations as $location) {
//        if ($list_temp[$location->item_id]->sponsored) {
//          $this->view->flageSponsored = 1;
//          break;
//        }
//      }
//
//      $this->view->list = $list_temp;
//    } else {
//      $this->view->enableLocation = 0;
//    }
//    $view = $this->view;
//    $view->addHelperPath(APPLICATION_PATH . '/application/modules/Fields/View/Helper', 'Fields_View_Helper');
//    $this->view->search = 0;
//    if (!empty($_GET))
//      $this->view->search = 1;
//
//    $this->view->can_create = Engine_Api::_()->authorization()->isAllowed('list_listing', $viewer, 'create');
//    if (empty($list_browse)) {
//      return $this->setNoRender();
//    }
  }

}
